package org.jeecg.modules.gift.controller;


import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.gift.entity.GiftProductInfo;
import org.jeecg.modules.gift.entity.GiftUserCollect;
import org.jeecg.modules.gift.service.IGiftUserCollectService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;


import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 用户收藏
 * @Author: jeecg-boot
 * @Date:   2024-07-12
 * @Version: V1.0
 */
@Api(tags="用户收藏")
@RestController
@RequestMapping("/gift/giftUserCollect")
@Slf4j
public class GiftUserCollectController extends JeecgController<GiftUserCollect, IGiftUserCollectService> {
	@Autowired
	private IGiftUserCollectService giftUserCollectService;

	 /**
	  * 分页列表查询
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	@ApiOperation(value="用户收藏-分页列表查询", notes="用户收藏-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<GiftProductInfo>> queryPageList(
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userId = sysUser.getId();
		Page<GiftProductInfo> page = new Page<GiftProductInfo>(pageNo, pageSize);
		IPage<GiftProductInfo> pageList = giftUserCollectService.selectPageUserCollectList(page, userId);
		return Result.OK(pageList);
	}


	 /**
	  *
	  * @param productId
	  * @return
	  */
	@ApiOperation(value="用户收藏 添加/取消", notes="用户收藏 添加/取消")
	@PostMapping(value = "/operate")
	public Result<String> add(@RequestParam(name="productId",required=true) String productId) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userId = sysUser.getId();

		LambdaQueryWrapper<GiftUserCollect> query = new LambdaQueryWrapper<GiftUserCollect>();
		query.eq(GiftUserCollect::getUserId,userId);
		query.eq(GiftUserCollect::getProductId,productId);
		if(giftUserCollectService.count(query)>0){
			giftUserCollectService.remove(query);
			return Result.OK("取消成功！");
		}else{
			GiftUserCollect giftUserCollect = new GiftUserCollect();
			giftUserCollect.setUserId(userId);
			giftUserCollect.setProductId(productId);
			giftUserCollectService.save(giftUserCollect);
			return Result.OK("添加成功！");
		}
	}
	

}
